package cn.bopomo.dealer.base.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuthorizeList;
import cn.bopomo.dealer.base.vo.ModuleList;

public class ModuleAuthorizeAddAction extends BaseAction {

	private String codeDescXMLPath = "/WEB-INF/code_description.xml";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		StringBuffer result = new StringBuffer();
		
		if (!this.hasPermission(request)){
			result.append(ResultCode.OP_NO_PRIMITION).append("||").append("你没有该操作权限，请与管理员联系");
			response.getWriter().write(result.toString());
			return null;
		}
		
		String authorize_name = request.getParameter("authorize_name");
		String authorize_tag = request.getParameter("authorize_tag");
		String authorize_action = request.getParameter("authorize_action");
		String module_authorize = request.getParameter("module_id");
		AuthorizeList authorize = new AuthorizeList();
		
		ModuleList module = new ModuleList();
		module.setMid(new Integer(module_authorize));
		authorize.setModuleList(module);
		authorize.setAddDate(new Date());
		authorize.setAddUid(Integer.parseInt(this.getUserId(request)));
		authorize.setAuthorizeAction(authorize_action == null ? "" : authorize_action);
		authorize.setAuthorizeName(authorize_name == null ? "" : authorize_action);
		authorize.setAuthorizeTag(authorize_tag == null ? "" : authorize_tag);
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		//检查是否有重名的权限操作......
		String checkResult = os.checkAuthorizeValidate(authorize);
		
		if(checkResult.equals(ResultCode.OP_SUCCESS)){
			checkResult = os.addAuthorize(authorize);
		}
		result.append(checkResult).append("||").append(this.getCodeDescription(checkResult,request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
		response.getWriter().write(result.toString());
		return null;
		
	}
	
	
	
}
